var mysql = require('../lib/mysql');
function randomWord(randomFlag, min, max){
    var str = "",
        range = min,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
 
    // 随机产生
    if(randomFlag){
        range = Math.round(Math.random() * (max-min)) + min;
    }
    for(var i=0; i<range; i++){
        pos = Math.round(Math.random() * (arr.length-1));
        str += arr[pos];
    }
    return str;
}
class Shop{
  constructor(){
      //获取文章的总数
          this.totalCount = this.getTotalCount();
          //设置每页的数量
          this.count = 5;
  } 
  getToken(){
      var token = randomWord(false, 24);
      return token;
  }
  async getTotalCount(){
        return mysql.table('shopList').count('id');
  }
  findAll(page){
    var self = this;
    // offset Number 起始位置
    var page = page?page:1;
    var offset = (page-1)*(self.count);
    // Number 查询的数目
    var length = self.count
    return mysql.table('shopList').order('id DESC').limit(offset,self.count).select();
  }
  findOne(id){
    return mysql.table('shopList').where({id}).select();
  }
  add(params){
    return mysql.table('shopList').add(params);
  }
  deleteOne(id){
    return mysql.table('shopList').where({id}).delete();
  }
  update(id,data){
    return mysql.table('shopList').where({id}).update(data)
  }

}
module.exports = Shop;